package cn.tedu.order.service.impl;

import cn.tedu.order.entity.Order;
import cn.tedu.order.feign.AccountClient;
import cn.tedu.order.feign.EasyIdClient;
import cn.tedu.order.feign.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import cn.tedu.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private EasyIdClient easyIdClient;

    @Autowired
    private AccountClient accountClient;

    @Autowired
    private StorageClient storageClient;

    @GlobalTransactional    //Seata全局事务
    @Transactional          //本地事务
    @Override
    public void create(Order order) {
        //生成订单id
        //远程调用全局唯一id发号器
        String s = easyIdClient.nextId("order_business");
        Long orderId = Long.valueOf(s);

        order.setId(orderId);

        //保存订单
        orderMapper.create(order);

        // TODO: 调用storage，修改库存
      //  storageClient.decrease(order.getProductId(),order.getCount());
        // TODO: 调用account，修改账户余额
      //  accountClient.decrease(order.getUserId(), order.getMoney());
    }
}
